package com.example.forum.services.impl;

import com.example.forum.common.AppResult;
import com.example.forum.common.ResultCode;
import com.example.forum.dao.MessageMapper;
import com.example.forum.exception.ApplicationException;
import com.example.forum.model.Message;
import com.example.forum.model.User;
import com.example.forum.services.IMessageService;
import com.example.forum.services.IUserService;
import com.example.forum.utils.StringUtil;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class MessageServiceImpl implements IMessageService {
    @Resource
    private MessageMapper messageMapper;
    @Resource
    private IUserService userService;
    @Override
    public void create(Message message) {
        //非空校验
        if(message==null
           ||StringUtil.isEmpty(message.getContent())
           ||message.getPostUserId()==null
           ||message.getReceiveUserId()==null){
            // 打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            // 抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //检验接收者是否存在
        User user=userService.selectById(message.getReceiveUserId());
        if(user==null){
            // 抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //设置默认值
        message.setState((byte)0);
        message.setDeleteState((byte)0);
        //设置创建与更新时间
        Date date=new Date();
        message.setCreateTime(date);
        message.setUpdateTime(date);
        //调用dao
        int row=messageMapper.insertSelective(message);
        if (row != 1) {
            log.warn(ResultCode.FAILED_CREATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_CREATE));
        }
    }

    @Override
    public Integer selectUnreadCount(Long receiveUserId) {
        //非空校验
        if(receiveUserId==null||receiveUserId<=0){
            // 打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            // 抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //直接调用Dao
        Integer count= messageMapper.selectUnreadCount(receiveUserId);
        //正差查询不可能出现null
        if(count==null){
            log.warn(ResultCode.ERROR_SERVICES.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_SERVICES));
        }
        //返回结果
        return count;
    }

    @Override
    public List<Message> selectByReceiveUserId(Long receiveUserId) {
        //检验参数
        if(receiveUserId==null||receiveUserId<=0){
            //打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //调用DAO
        List<Message>messages=messageMapper.selectByReceiveUserId(receiveUserId);
        //返回结果
        return messages;
    }

    @Override
    public void updateStateById(Long id, Byte state) {
        //检验参数
         if(id==null||id<=0){
             //打印日志
             log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
             //抛出异常
             throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
         }
        //查询有无此消息
        Message message=messageMapper.selectByPrimaryKey(id);
        if(message.getDeleteState()==1){
            log.warn("该站内信");
            throw  new ApplicationException(AppResult.failed(ResultCode.FAILED_NOT_EXISTS));
        }
        //构造新对象
        Message updateMessage=new Message();
        updateMessage.setId(id);
        updateMessage.setState(state);
        Date date=new Date();
        updateMessage.setUpdateTime(date);
        //调用Dao
        int row=messageMapper.updateByPrimaryKeySelective(updateMessage);
        if (row != 1) {
            log.warn(ResultCode.ERROR_SERVICES.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_SERVICES));
        }
    }

    @Override
    public Message selectById(Long id) {
        if(id==null||id<=0){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        Message message=messageMapper.selectByPrimaryKey(id);
        if(message==null||message.getDeleteState()==1){
            log.warn("该回复不存在");
            throw new ApplicationException(AppResult.failed("该回复已删除"));
        }
        return message;
    }

    @Override
    public void reply(Long repliedId, Message message) {
        //非空检验
        if(repliedId==null||repliedId<=0){
            // 打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            // 抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //校验repliedId对应的站内信状态
        Message existsMessage=messageMapper.selectByPrimaryKey(repliedId);
        if(existsMessage==null||existsMessage.getDeleteState()==1){
            // 打印日志
            log.warn(ResultCode.FAILED_MESSAGE_NOT_EXISTS.toString());
            // 抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_MESSAGE_NOT_EXISTS));
        }
        //更新状态为已回复
        updateStateById(repliedId,(byte)2);
        create(message);
    }
}
